package com.itheima.web.controller.pms;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.service.pms.ProductCategoryAttributeRelationService;
import com.itheima.service.pms.ProductCategoryService;
import com.itheima.utils.PageResult;
import com.itheima.utils.ResultData;
import com.itheima.vo.pms.PmsProductCategory;
import com.itheima.vo.pms.PmsProductCategoryAttributeRelation;
import com.itheima.vo.pms.ProductCategoryDto;
import com.itheima.vo.pms.ProductCategoryVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@RestController
@RequestMapping("productCategory")
public class ProductCategoryController {

    @DubboReference
    private ProductCategoryService productCategoryService;

    @DubboReference
    private ProductCategoryAttributeRelationService parService;

    /**
     * 添加商品分类
     */
    @PostMapping("create")
    @Transactional
    public ResultData create(@RequestBody ProductCategoryVo productCategoryVo) {
        //添加它本身的表
        PmsProductCategory pmsProductCategory = new PmsProductCategory();
        BeanUtils.copyProperties(productCategoryVo, pmsProductCategory);
        if (pmsProductCategory.getLevel()==null){
            pmsProductCategory.setLevel(0);
        }

        if (pmsProductCategory.getProductCount()==null){
            pmsProductCategory.setProductCount(0);
        }

        if (Objects.equals(pmsProductCategory.getProductUnit(), "")){
            pmsProductCategory.setProductUnit("件");
        }

        //先查询名称是否重复
        PmsProductCategory productCategory=productCategoryService.getByName(pmsProductCategory.getName());
        if (productCategory!=null){
            return ResultData.error("名称已存在");
        }

        boolean save = productCategoryService.save(pmsProductCategory);
        if (!save) {
            return ResultData.error("添加异常");
        }

        PmsProductCategoryAttributeRelation ppp = new PmsProductCategoryAttributeRelation();

        List<Integer> idList = productCategoryVo.getProductAttributeIdList();
        if (idList.size() == 0){
            return ResultData.ok("操作成功", null);
        }

        for (Integer id : idList) {
            ppp.setProductCategoryId(productCategory.getId());
            ppp.setProductAttributeId(id);
            parService.save(ppp);
            return ResultData.ok("操作成功", null);
        }

        return ResultData.ok("操作成功", null);
    }

    /**
     * 查询所有一级分类及子分类
     */
    @GetMapping("list/withChildren")
    public ResultData withChildren() {
        //先查询到所有的一级分类
        List<PmsProductCategory> list = productCategoryService.selectByParentId(0);

        List<ProductCategoryDto> dtoList = list.stream().map((item) -> {
            ProductCategoryDto productCategoryDto = new ProductCategoryDto();
            BeanUtils.copyProperties(item, productCategoryDto);

            //通过id查询到所有的子分类
            List<PmsProductCategory> categoryList = productCategoryService.selectByParentId(item.getId());
            productCategoryDto.setChildren(categoryList);

            return productCategoryDto;
        }).collect(Collectors.toList());

        return ResultData.ok("操作成功", dtoList);
    }

    @GetMapping("list/{parentId}")
    public ResultData list(@PathVariable Integer parentId, int pageNum, int pageSize) {
        long count = productCategoryService.countByParentId(parentId);
        pageNum=(pageNum-1)*pageSize;
        List<PmsProductCategory> list = productCategoryService.pageByparentId(parentId, pageNum, pageSize);

        PageResult init = PageResult.init(count, (long) pageSize, (long) pageNum, list);
        return ResultData.ok("操作成功", init);
    }

    @PostMapping("/delete/{id}")
    public ResultData create(@PathVariable Integer id) {
        productCategoryService.removeById(id);
        return ResultData.ok("操作成功", null);
    }

    @PostMapping("/update/navStatus")
    public ResultData navStatus(@RequestParam List<Integer> ids, Integer navStatus) {
        productCategoryService.updateByIds(ids, navStatus);
        return ResultData.ok("操作成功", null);
    }

    @PostMapping("/update/showStatus")
    public ResultData showStatus(@RequestParam List<Integer> ids, Integer showStatus) {
        productCategoryService.updateByIds2(ids, showStatus);
        return ResultData.ok("操作成功", null);
    }

    @GetMapping("{id}")
    public ResultData getById(@PathVariable Integer id){
        PmsProductCategory byId = productCategoryService.getById(id);
        return ResultData.ok("查询成功",byId);
    }

    @PostMapping("/update/{id}")
    @Transactional
    public ResultData updateById(@PathVariable Integer id,@RequestBody ProductCategoryVo productCategoryVo){
        //修改原先的表中数据
        PmsProductCategory pmsProductCategory=new PmsProductCategory();
        BeanUtils.copyProperties(productCategoryVo,pmsProductCategory);
        pmsProductCategory.setId(id);
        productCategoryService.updateById(pmsProductCategory);

        //先删除关联表中的信息
        parService.removeBy(id);

        //在修改关联的表中数据
        List<Integer> productAttributeIdList = productCategoryVo.getProductAttributeIdList();

        if (productAttributeIdList.size() == 0){
            return ResultData.ok("操作成功", null);
        }

        for (Integer integer : productAttributeIdList) {
            PmsProductCategoryAttributeRelation ppca=new PmsProductCategoryAttributeRelation();
            ppca.setProductCategoryId(id);
            ppca.setProductAttributeId(integer);
            parService.save(ppca);
        }

        return ResultData.ok("操作成功",null);
    }

}
